<?php

class Application_Model_RequestMapper{

	protected $_dbTable;

	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_Request');
		}
		return $this->_dbTable;
	}
	
	public function save(Application_Model_Request $request){
	
	
		$data = array(
				'keyword' 			=> $request->getKeyword(),
				'class'				=> $request->getClass(),
				'offerClass'		=> $request->getOfferClass(),
				'options'			=> $request->getOptionss(),
				'admin_area_lvl1'	=> $request->getAdmin_area_lvl1(),
				'admin_area_lvl2'	=> $request->getAdmin_area_lvl2(),
				'country'			=> $request->getCountry(),
				'price_interval'	=> $request->getPrice_interval(),
				'id_user'			=> $request->getUser()->getUserId(),
				'date'				=> $request->getDate(),
		);
	
		if (null === ($id = $request->getRequestId())) {
			unset($data['id_request']);
			return $this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id_request = ?' => $id));
			return $id;
		}
	}
	
	public function find($id, Application_Model_Request $request)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$request->setRequestId($row->id_request);
		$request->setKeyword($row->keyword);
		$request->setOptionss($row->options);
		$request->setClass($row->class);
		$request->setOfferClass($row->offerClass);
		$request->setAdmin_area_lvl1($row->admin_area_lvl1);
		$request->setAdmin_area_lvl2($row->admin_area_lvl2);
		$request->setCountry($row->country);
		$request->setPrice_interval($row->price_interval);
		$request->setDate($row->date);
		$usermapper = new Application_Model_UserMapper();
		$user = $usermapper->find($row->id_user);
		$request->setUser($user);
		return $request;
	}
	
	public function findAll($where, $order = null, $count = null, $offset = null){
		$rows = $this->getDbTable()->fetchAll($where,$order,$count,$offset);
		if (0 == count($rows)) {
			return null;
		}
		$requests = array();
		
		foreach ($rows as $row){
			$request = new Application_Model_Request();
			//@TODO Attention au comportement de la boucle sinon ajouter $row = $result->current();
			$request->setRequestId($row->id_request);
			$request->setKeyword($row->keyword);
			$request->setOptionss($row->options);
			$request->setClass($row->class);
			$request->setAdmin_area_lvl1($row->admin_area_lvl1);
			$request->setAdmin_area_lvl2($row->admin_area_lvl2);
			$request->setCountry($row->country);
			$request->setPrice_interval($row->price_interval);
			$request->setDate($row->date);
			$usermapper = new Application_Model_UserMapper();
			$user = $usermapper->find($row->id_user);
			$request->setUser($user);
			$requests[] = $request;
		}
		
		return $requests;
	}
	
	public function delete($id)
	{
	
		$result = $this->getDbTable()->find($id);
		if (0 != count($result)) {
			$this->getDbTable()->delete(array('id_request = ?' => $id));
		}
	
	}
}